<?php

require_once(dirname(__FILE__)."/AutoForm.class.php");



header("content-type:text/html;charset=utf-8");

include(dirname(__FILE__)."/Mysql.class.php");

//$db = new Mysql("localhost", "root", "123456", "noval");

//$rows = $db->getRows("SELECT id, name FROM category");
//$options = array();
//foreach($rows as $row){
//    $options[] = $row['name'];
//}

$formAttr = array(
    "attr" => "name='30' aciton='{$_SERVER[PHP_SELF]}' method='post'",
    "capture" => "添加文章",
    "td" => 3, //三列式显示，第三列用来显示表单输入提示, 各项的提示语用 teller 来赋值
    );

$fieldsSettings = array(
    //article 是表名
    "article" => array(
        "title:标题" => array(
            "value" => "输入文章标题",
            "title" => "你的标题啊",
            "_validator" => array("NotNull", "MinLength=3"),
            //没有指定 type 属性, 默认为 text
        ),

        "content:内容" => array(
            "value" => "请输入文章内容", //默认文本域内容
            "type" => "textarea",
            "rows" => 10,
            "cols" => 40,
            "teller" => "请输入你的内容",
            "_validator" => array("NotNull", array("return strpos({:value},'共产党')!==false" => "不能包含共产党三个字")),
            "_filter" => array("trim", "htmlspecialchars", "substr({:value}, 0, 30)", "my" => "{:value}=str_replace('hello', 'hi', {:value})")
        ),

        "access_password:浏览密码" => array(
            "type" => "password",
            "teller" => "请输入你的密码",
            "_validator" => array(array("return false" => "你输入的密码不正确"))
        ),

        "category:分类" => array(
            "type" => "select",
            "options" => array(
				"大学文章" => "大学文章", "高中文章" => "高中文章", "小学文章" => "小学文章"
			),
            "value" => "小学文章" //默认选中"小学文章"
        ),

       "access:隐私设置" => array(
            "type" => "radioGroup",
            "options" => array(
                "1" => "公开",
                "2" => "好友",
                "3" => "私密"
            ),
            "value" => "3" //默认选中"公开"
         ),

        "search_engine:提交到" => array(
            "type" => "checkboxGroup",
            "options" => array(
                "1" => "百度",
                "2" => "Google",
                "3" => "Yahoo",
                "4" => "搜狗"
            ),
            "value" => array("2", "1"), //默认选中 "百度", "Google", "搜狗"
            "_validator" => array(array("return empty({:value})" => "你必须选中其中一项")),
        ),

        "send_to:发送给" => array(
            "_validator" => array("NotNull", "Email")
        ),
    ),

    "category" => array(
        "id:分类ID" => array(
            "size" => 1,
            //"disabled" => "disabled",
            "value" => 3
        ),

        "name:分类名称" => array(
            "value" => "大学时代",
            "_validator" => array("MinLength=4", "MaxLength=9"),
        )
    ),
);

try{
    $form = new AutoForm($formAttr, $fieldsSettings);
    $form->checkSubmit($_POST);
    $form->receiveSubmit($_POST);
    echo $form;
}catch(DebugException $e){
    echo "Debug: " . $e->getMessage();
    echo "<br />";
    echo "Line: " . $e->getLine();
}
